local youyan = fk.CreateSkill {
  name = "gai__youyan",
  max_branches_use_time = {
    ["youyan_play"] = {
      [Player.HistoryTurn] = 1
    },
    ["youyan_discard"] = {
      [Player.HistoryTurn] = 1
    },
  }
}

Fk:loadTranslationTable{
  ["gai__youyan"] = "诱言",
  [":gai__youyan"] = "出牌阶段限一次，你可以弃置任意张牌；你的回合内，当你的牌因使用或打出之外的方式进入弃牌堆后，你可以从牌堆中获得本次失去的牌中没有的花色的牌各一张"..
  "（出牌阶段、弃牌阶段各限一次）。",

  ["#gai__youyan"] = "诱言:你可以弃置任意张牌。",

  ["$gai__youyan1"] = "诱言者，为人所不齿。",
  ["$gai__youyan2"] = "诱言之弊，不可不慎。",
}

youyan:addEffect("active", {
  anim_type = "drawcard",
  min_card_num = 1,
  target_num = 0,
  prompt = "#gai__youyan",
  max_phase_use_time = 1,
  card_filter = function(self, player, to_select, selected)
    return not player:prohibitDiscard(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:throwCard(effect.cards, youyan.name, player, player)
  end
})

youyan:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(youyan.name) and
      (player.phase == Player.Play or player.phase == Player.Discard) then
      local branch = (player.phase == Player.Play) and "youyan_play" or "youyan_discard"
      if youyan:withinBranchTimesLimit(player, branch) then
        local suits = {"spade", "club", "heart", "diamond"}
        local can_invoke = false
        for _, move in ipairs(data) do
          if move.toArea == Card.DiscardPile and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResponse then
            if move.from == player then
              for _, info in ipairs(move.moveInfo) do
                if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
                  table.removeOne(suits, Fk:getCardById(info.cardId):getSuitString())
                  can_invoke = true
                end
              end
            else
              local room = player.room
              local pindian_event = player.room.logic:getCurrentEvent():findParent(GameEvent.Pindian, true)
              if pindian_event then
                local pindian = pindian_event.data
                if pindian.from == player then
                  local cards = room:getSubcardsByRule(pindian.fromCard)
                  for _, info in ipairs(move.moveInfo) do
                    if info.fromArea == Card.Processing and table.contains(cards, info.cardId) then
                      table.removeOne(suits, Fk:getCardById(info.cardId):getSuitString())
                      can_invoke = true
                    end
                  end
                end
                for to, result in pairs(pindian.results) do
                  if to == player then
                    local cards = room:getSubcardsByRule(result.toCard)
                    for _, info in ipairs(move.moveInfo) do
                      if info.fromArea == Card.Processing and table.contains(cards, info.cardId) then
                        table.removeOne(suits, Fk:getCardById(info.cardId):getSuitString())
                        can_invoke = true
                      end
                    end
                  end
                end
              end
            end
          end
        end
        if can_invoke and #suits > 0 then
          event:setCostData(self, {choice = suits})
          return true
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local suits = event:getCostData(self).choice
    local branch = (player.phase == Player.Play) and "youyan_play" or "youyan_discard"
    player:addSkillBranchUseHistory(youyan.name, branch)
    local cards = {}
    while #suits > 0 do
      local pattern = table.random(suits)
      table.removeOne(suits, pattern)
      table.insertTable(cards, room:getCardsFromPileByRule(".|.|"..pattern))
    end
    if #cards > 0 then
      room:moveCards({
        ids = cards,
        to = player,
        toArea = Card.PlayerHand,
        moveReason = fk.ReasonJustMove,
        proposer = player,
        skill_name = youyan.name,
      })
    end
  end,
})

return youyan